/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycompany.psc.entities;

import com.mycompany.psc.data.TenderType;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 *
 * @author A7med
 */
@Entity
@Table(name = "tender_items")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "TenderItems.findAll", query = "SELECT t FROM TenderItems t"),
    @NamedQuery(name = "TenderItems.findByItemId", query = "SELECT t FROM TenderItems t WHERE t.itemId = :id"),
    @NamedQuery(name = "TenderItems.findByTitle", query = "SELECT t FROM TenderItems t WHERE t.title = :title"),
    @NamedQuery(name = "TenderItems.findByTitleAndProject", query = "SELECT t FROM TenderItems t WHERE t.title = :title and t.project=:project"),
    @NamedQuery(name = "TenderItems.findByCodeAndProject", query = "SELECT t FROM TenderItems t WHERE t.code = :code and t.project=:project"),
    @NamedQuery(name = "TenderItems.findByType", query = "SELECT t FROM TenderItems t WHERE t.type = :type"),
    @NamedQuery(name = "TenderItems.findBySubmitDate", query = "SELECT t FROM TenderItems t WHERE t.submitDate = :submitDate"),
    @NamedQuery(name = "TenderItems.findByAuthor", query = "SELECT t FROM TenderItems t WHERE t.author = :author"),
    @NamedQuery(name = "TenderItems.findAllInProject", query = "SELECT t FROM TenderItems t WHERE t.project = :project and t.type=:type")})
public class TenderItems implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "item_id")
    private Integer itemId;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 255)
    @Column(name = "title")
    private String title;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 255)
    @Column(name = "code")
    private String code;
    @Column(name = "type")
    @Enumerated(EnumType.STRING)
    private TenderType type;
    @Basic(optional = false)
    @NotNull
    @Column(name = "submit_date")
    @Temporal(TemporalType.TIMESTAMP)
    private Date submitDate;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 255)
    @Column(name = "author")
    private String author;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "parent")
    private List<TenderItemsData> tenderItemsDataList;
    @JoinColumn(name = "project", referencedColumnName = "id")
    @ManyToOne(optional = false)
    private Projects project;
    @Basic(optional = false)
    @NotNull
    @Column(name = "expences")
    private float expences;
    @Basic(optional = false)
    @NotNull
    @Column(name = "amount")
    private int amount;

    public int getAmount() {
        return amount;
    }

    public void setAmount(int amount) {
        this.amount = amount;
    }

    public TenderItems() {
    }

    public TenderItems(Integer itemId) {
        this.itemId = itemId;
    }

    public TenderItems(Integer itemId, String title, TenderType type, Date submitDate, String author) {
        this.itemId = itemId;
        this.title = title;
        this.type = type;
        this.submitDate = submitDate;
        this.author = author;
    }

    public Integer getItemId() {
        return itemId;
    }

    public void setItemId(Integer itemId) {
        this.itemId = itemId;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public TenderType getType() {
        return type;
    }

    public void setType(TenderType type) {
        this.type = type;
    }

    public Date getSubmitDate() {
        return submitDate;
    }

    public void setSubmitDate(Date submitDate) {
        this.submitDate = submitDate;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    @XmlTransient
    public List<TenderItemsData> getTenderItemsDataList() {
        return tenderItemsDataList;
    }

    public void setTenderItemsDataList(List<TenderItemsData> tenderItemsDataList) {
        this.tenderItemsDataList = tenderItemsDataList;
    }

    public Projects getProject() {
        return project;
    }

    public void setProject(Projects project) {
        this.project = project;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (itemId != null ? itemId.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof TenderItems)) {
            return false;
        }
        TenderItems other = (TenderItems) object;
        if ((this.itemId == null && other.itemId != null) || (this.itemId != null && !this.itemId.equals(other.itemId))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.mycompany.psc.entities.TenderItems[ itemId=" + itemId + " ]";
    }

    public double getTotalValue() {
        double totalValue = 0;
        for (TenderItemsData t : tenderItemsDataList) {
            totalValue += t.getUnitTotal();
        }
        return totalValue;
    }

    public float getExpences() {
        return expences;
    }

    public void setExpences(float expences) {
        this.expences = expences;
    }

    public int getTotalPrice() {
        double totalValue = getTotalValue();
        return (int) (totalValue + ((expences / 100) * totalValue));
    }

    public long getFinalPrice() {
        return getTotalPrice() * amount;
    }
    public TenderItems createCopy(Projects target){
        TenderItems copy=new TenderItems();
        copy.setAmount(amount);
        copy.setAuthor(author);
        copy.setCode(code);
        copy.setExpences(expences);
        copy.setProject(target);
        copy.setType(type);
        copy.setSubmitDate(new Date());
        copy.setTitle(title);
        return copy;
    }
    public void copyChildren(TenderItems target){
         for(TenderItemsData offer:tenderItemsDataList){
            TenderItemsData newTenderItem=offer.crateCopy();
            newTenderItem.setParent(target);
            target.getTenderItemsDataList().add(newTenderItem);            
        }
    }
}
